//
//  TSDetailRecordViewController.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/19.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit
import MJRefresh

class TSDetailRecordViewController: TSBaseViewController {

    var type : String!
    var tableController : TSBaseTableViewController!
    var dataSource : [TSDetailRecordModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupNavigationTitle(title: "开奖记录")
        configViews()
        requestDatas()
    }

    @objc private func refreshDatas() {
        
        requestDatas()
    }
}

//MARK: - UITableViewDelegate
extension TSDetailRecordViewController : UITableViewDelegate {
    
}

//MARK: - UITableViewDataSource
extension TSDetailRecordViewController : UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(TSDetailRecordTableViewCell.self)) as? TSDetailRecordTableViewCell
        
        if cell == nil {
            cell = TSDetailRecordTableViewCell.init(style: .default, reuseIdentifier: NSStringFromClass(TSDetailRecordTableViewCell.self))
        }
        
        let last = indexPath.row == dataSource.count - 1
        cell?.setupCell(model: dataSource[indexPath.row], last: last)
        
        return cell!
    }
}

//MARK: - Network Request
extension TSDetailRecordViewController {
    
    func requestDatas() {
        
        CBToast.showToastAction()
        if type == "北京赛车" {
            TSNetworkManager.request(TMSNetworkTools.getRacingResultInfo, showHUD: true, success: { (data) in

                CBToast.hiddenToastAction()
                self.tableController.tableView.mj_header.endRefreshing()
                
                self.dataSource.removeAll()
                let array = data as? Array<Dictionary<String, Any>>
                if array?.count ?? 0 > 0 {
                    for item in array! {
                        var tempModel = TSDetailRecordModel.deserialize(from: item)
                        tempModel?.title = self.type
                        if let model = tempModel {
                            self.dataSource.append(model)
                        }
                    }
                }
                self.tableController.tableView.reloadData()
                
            }) { (error) in
                
                CBToast.hiddenToastAction()
                self.tableController.tableView.mj_header.endRefreshing()
            }
        } else {
            TSNetworkManager.request(TMSNetworkTools.getYachtResultInfo, showHUD: true, success: { (data) in
                
                CBToast.hiddenToastAction()
                self.tableController.tableView.mj_header.endRefreshing()
                
                self.dataSource.removeAll()
                let array = data as? Array<Dictionary<String, Any>>
                if array?.count ?? 0 > 0 {
                    for item in array! {
                        var tempModel = TSDetailRecordModel.deserialize(from: item)
                        tempModel?.title = self.type
                        if let model = tempModel {
                            self.dataSource.append(model)
                        }
                    }
                }
                self.tableController.tableView.reloadData()
                
            }) { (error) in
                
                CBToast.hiddenToastAction()
                self.tableController.tableView.mj_header.endRefreshing()
            }
        }
    }
}

//MARK: - Initialize
extension TSDetailRecordViewController {
    
    func configViews() {
        
        tableController = TSBaseTableViewController(style: .plain)
        tableController.emptyAlertText = "暂无记录"
        tableController.tableView.backgroundColor = WHITE_COLOR
        tableController.tableView.delegate = self;
        tableController.tableView.dataSource = self
        tableController.tableView.separatorStyle = .none
        tableController.tableView.estimatedRowHeight = 100
        tableController.tableView.rowHeight = UITableView.automaticDimension
        self.view.addSubview(tableController.tableView)
        tableController.tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(T_NAVBARHEIGHT)
        }
        
        tableController.tableView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(refreshDatas))
    }
}
